package org.jeecg.modules.dlxj.service.impl;

import org.jeecg.modules.dlxj.entity.DlxjCircuitry;
import org.jeecg.modules.dlxj.entity.DlxjPole;
import org.jeecg.modules.dlxj.mapper.DlxjCircuitryMapper;
import org.jeecg.modules.dlxj.mapper.DlxjPatrtolTaskMapper;
import org.jeecg.modules.dlxj.service.IDlxjCircuitryService;
import org.jeecg.modules.dlxj.vo.CirNameVo;
import org.jeecgframework.minidao.util.SnowflakeIdWorker;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 * @Description: 线路表
 * @Author: lzl
 * @Date:   2022-07-20
 * @Version: V1.0
 */
@Service
public class DlxjCircuitryServiceImpl extends ServiceImpl<DlxjCircuitryMapper, DlxjCircuitry> implements IDlxjCircuitryService {

    @Autowired
    private DlxjCircuitryMapper dlxjCircuitryMapper;

    /**
     * 通过线路id获得线路名称
     * @param cirIds
     * @return
     */
    @Override
    public Map<String, String> getCirNameByIds(List<String> cirIds) {
            List<CirNameVo> list = this.baseMapper.getCirNameByIds(cirIds);
            Map<String, String> res = new HashMap(5);
            list.forEach(item -> {
                        if (res.get(item.getCirCode()) == null) {
                            res.put(item.getCirCode(), item.getCirName());
                        } else {
                            res.put(item.getCirCode(), res.get(item.getCirCode()) + "," + item.getCirName());
                        }
                    }
            );
            return res;}

    /**
     * 通过线路id，删除杆塔，线路关联表的相关信息
     * @param circuitryCode
     */
    @Override
    public void deleteCirAndPole(String circuitryCode) {
        dlxjCircuitryMapper.deleteCirAndPole(circuitryCode);

    }

    /**
     * 线路下拉
     * @return
     */
    @Override
    public List<DlxjCircuitry> selectCircuitry() {
        return dlxjCircuitryMapper.selectCircuitry();
    }

    @Override
    public void bindingPoleAndCir(DlxjCircuitry dlxjCircuitry) {
        Integer startPosition = dlxjCircuitryMapper.getPosition(dlxjCircuitry.getStartPoleNo());
        Integer endPosition = dlxjCircuitryMapper.getPosition(dlxjCircuitry.getEndPoleNo());
        int poleCount = endPosition - startPosition;

        String belongCirName = dlxjCircuitry.getName();
        String startPole = dlxjCircuitry.getStartPoleNo();
        int state = 1;

        String[] strs = startPole.split("[^0-9]");//根据不是数字的字符拆分字符串
        String numStr = strs[strs.length - 1];//取出最后一组数字
        dlxjCircuitryMapper.insertPoles(UUID.randomUUID().toString(),startPole);
        for (int i = 0; i < poleCount; i++) {
            String id = UUID.randomUUID().toString();
            if (numStr != null && numStr.length() > 0) {//如果最后一组没有数字(也就是不以数字结尾)，抛NumberFormatException异常
                int n = numStr.length();//取出字符串的长度
                int num = Integer.parseInt(numStr) + 1;//将该数字加一
                String added = String.valueOf(num);
                n = Math.min(n, added.length());
                //拼接字符串
                String nextPole = startPole.subSequence(0, startPole.length() - n) + added;
                String[] nextPoles = nextPole.split("[^0-9]");
                numStr = nextPoles[nextPoles.length - 1];
                dlxjCircuitryMapper.insertPoles(id,nextPole);

            } else {
                throw new NumberFormatException();
            }

        }

    }


}
